<?php
/******************************************************
v4 - Management Framework
Copyright (C) 2008  Gary Taylor, gMerc Incorporation
http://www.gmerc.com/v4/

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

gary@gmerc.com
******************************************************/
class quote {
  function loaddata() {
    global $masterclass;

    $masterclass->loadmodule('customers');
    $masterclass->loadmodule('personnel');
    $masterclass->loadmodule('invoices');
    $masterclass->loadmodule('companies');

    $masterclass->loadjavascript('invoices');

    $masterclass->websiteconfig['javascript_include'][]='include/date-picker.js';
    
    $masterclass->loadjavascript('',$masterclass->mainconfig['v4rootdir'].'include/suckertreemenu.inc.js');

    $masterclass->websiteconfig['title']='Quote';
    $tmpdocid=$masterclass->allclasses['invoices']->fetch_invoicesdocumentid($masterclass->sitevars['invoicesid']);
    $tmprecipient=$masterclass->allclasses['invoices']->fetch_invoicesrecipient($masterclass->sitevars['invoicesid']);
    if ($tmpdocid!='') { $masterclass->websiteconfig['title'].=' #'.$tmpdocid; }
    if ($tmprecipient!='') { $masterclass->websiteconfig['title'].=' - '.$tmprecipient; }
    
    if (!isset($masterclass->sitevars['invoicesid']) || $masterclass->sitevars['invoicesid']=='') {
      if ($masterclass->sitevars['customerid']>0) {
        $strwhere=sprintf(" `id`=%d ",$masterclass->format_dbl($masterclass->sitevars['customerid']));
        $customerinfo=$masterclass->fetch_records('`customers`',1,$strwhere);

        if (!empty($customerinfo->fields)) {
          $invoices_recipientlineone=$customerinfo->fields['first'].' '.$customerinfo->fields['last'];
          $invoices_recipientlinetwo=$customerinfo->fields['address'];
          $invoices_recipientlinethree=$customerinfo->fields['city'].', '.$customerinfo->fields['state'].'  '.$customerinfo->fields['zip'];
          $invoices_recipientlinefour=$customerinfo->fields['emaila'];
          $invoices_recipientlinefive=$customerinfo->fields['dayphone'];
        }
      }
      
      if ($masterclass->site['companyid']>0) {
        $strwhere=sprintf(" `id`=%d ",$masterclass->format_dbl($masterclass->site['companyid']));
        $companyinfo=$masterclass->fetch_records('`companies`',1,$strwhere);
        if (!empty($companyinfo->fields)) {

          $invoices_companylineone=$companyinfo->fields['name'];
          $invoices_companylinetwo=$companyinfo->fields['address'];
          $invoices_companylinethree=$companyinfo->fields['city'].', '.$companyinfo->fields['state'].' '.$companyinfo->fields['zip'];
          $invoices_companylinefour=$companyinfo->fields['phone'].'  '.$companyinfo->fields['mail'];
          $invoices_companylinefive=$companyinfo->fields['www'];
        }
      }
      
      $masterclass->sitevars['invoices_documentdate']=date("Y/m/d");
      $masterclass->sitevars['invoices_duedate']=date("Y/m/d");

      $lastdocid=$masterclass->allclasses['invoices']->fetch_invoices_lastdocid();
      $newdocid=$lastdocid+1;
      if ($lastdocid==$newdocid) { $newdocid=0; }


      $masterclass->site['postfields'][] = array(
        'table'=>'invoices_documents',
        'trackingid'=>'invoicesid',
        'fields'=>array(
          'customer'=>'customerid',
          'documentdate'=>'invoices_documentdate',
          'duedate'=>'invoices_duedate'
        ),
        'static'=>array(
          'active'=>'1',
          'personnel'=>$masterclass->site['personnelid'],
          'documentid'=>$newdocid,
          'documenttype'=>'quote',
          'net'=>'0',
          'recipientlineone'=>$invoices_recipientlineone,
          'recipientlinetwo'=>$invoices_recipientlinetwo,
          'recipientlinethree'=>$invoices_recipientlinethree,
          'recipientlinefour'=>$invoices_recipientlinefour,
          'recipientlinefive'=>$invoices_recipientlinefive,
          'companylineone'=>$invoices_companylineone,
          'companylinetwo'=>$invoices_companylinetwo,
          'companylinethree'=>$invoices_companylinethree,
          'companylinefour'=>$invoices_companylinefour,
          'companylinefive'=>$invoices_companylinefive
        )
      );

      echo $masterclass->savedata();

      $newlocation.=$masterclass->mainconfig['dir'].$masterclass->mainconfig['htmldirectory'].$masterclass->site['dirpath'].$masterclass->site['page'].".php?".$masterclass->trackingvar_query();
      header("Location: $newlocation");
      exit();
    }
    
    $masterclass->site['postfields'][] = array(
      'table'=>'invoices_documents',
      'trackingid'=>'invoicesid',
      'fields'=>array(
        'documentid'=>'invoices_documentid',
        
        'net'=>'invoices_net',

        'recipientlineone'=>'invoices_recipientlineone',
        'recipientlinetwo'=>'invoices_recipientlinetwo',
        'recipientlinethree'=>'invoices_recipientlinethree',
        'recipientlinefour'=>'invoices_recipientlinefour',
        'recipientlinefive'=>'invoices_recipientlinefive',
        
        'documentdate'=>'invoices_datehidden',
        'duedate'=>'invoices_duehidden'
      )
    );

    if ($masterclass->allclasses['invoices']->fetch_invoicesdocumenttype($masterclass->sitevars['invoicesid'])=='invoice') {
      header("Location: ".$masterclass->mainconfig['dir']."invoices/showinvoice.php?".$masterclass->trackingvar_query());
      exit();
    } else if ($masterclass->checkid('`invoices_documents`',$masterclass->sitevars['invoicesid'])=='') {
      echo '<div id="error_overlay"><div>INVOICE DOES NOT EXIST:<br><input type="button" OnClick="window.close();" value=" Close Window " class="button"></div></div>';
      exit();
    }
      
  return;
  }
  
  function savedata() {
    global $masterclass;

    foreach ($masterclass->sitevars['post_action'] as $postaction) {
      switch ($postaction) {
       case 'invoices_create':
        $masterclass->site['postfields'][] = array(
          'table'=>'invoices_documents',
          'trackingid'=>'invoicesid',
          'static'=>array(
            'documenttype'=>'invoice'
          )
        );
        $masterclass->savedata();
        $masterclass->site['forward']=$masterclass->mainconfig['htmldirectory'].'invoices/showinvoice.php?'.$masterclass->trackingvar_query();
       break;
       case 'invoices_save':
       
        $masterclass->site['postfields'][] = array(
          'table'=>'invoices_documents',
          'trackingid'=>'invoicesid',
          'static'=>array(
            'documenttype'=>'quote'
          )
        );
        $masterclass->savedata();
        $masterclass->site['forward']='window.close();';

       case 'invoices_saveitem':
         $this->save_invoiceitem($masterclass->sitevars['invoicesid'],
           $masterclass->sitevars['invoices_documentitem_new_qty'],
           $masterclass->sitevars['invoices_documentitem_new_cost'],
           $masterclass->sitevars['invoices_documentitem_new_taxed'],
           $masterclass->sitevars['invoices_documentitem_new_taxpercent'],     
           $masterclass->sitevars['invoices_documentitem_new_name'],
           $masterclass->sitevars['invoices_documentitem_new_description']
         );
         $masterclass->site['forward']='';
        break;
        case 'invoices_showpdf':
         $masterclass->site['forward']=$masterclass->mainconfig['htmldirectory'].'invoices/showinvoice_pdf.php?nohtml=1&'.$masterclass->trackingvar_query();
        break;
        case 'invoices_savepdfasfile':
         $masterclass->site['forward']=$masterclass->mainconfig['htmldirectory'].'invoices/saveinvoice_pdf.php?nohtml=1&'.$masterclass->trackingvar_query();
        break;
        case 'invoices_sendpdfemail':
         $masterclass->site['forward']=$masterclass->mainconfig['htmldirectory'].'invoices/showinvoice_emailpdf.php?'.$masterclass->trackingvar_query();
        break;
        case 'invoices_deleteinvoice':
        
         $deletefields=array();
         $strwhere=sprintf(" `id`=%d ",$masterclass->formatdbl($masterclass->sitevars['invoicesid']));
         $deletefields[] = array(
           'table'=>'invoices_documents',
           'strwhere'=>$strwhere
         );
         $strwhere=sprintf(" `invoices`=%d ",$masterclass->format_dbl($masterclass->sitevars['invoicesid']));
         $deletefields[] = array(
           'table'=>'invoices_documentitems',
           'strwhere'=>$strwhere
         );
         $masterclass->deletedata($deletefields);
         
         $masterclass->site['forward']='window.close();';
        break;
      }
    }

  return;
  }
  function save_invoiceitem($inv_id='',$inv_qty='',$inv_cost='',$inv_tax='',$inv_taxpercent='',$inv_item='',$inv_description='') {
    global $masterclass;
    
    if (isset($_POST['rsargs']) && !empty($_POST['rsargs'])) {
      $inv_id=$_POST['rsargs'][1];
      $inv_qty=$_POST['rsargs'][2];
      $inv_cost=$_POST['rsargs'][3];
      $inv_tax=$_POST['rsargs'][4];
      $inv_taxpercent=$_POST['rsargs'][5];
      $inv_item=$_POST['rsargs'][6];
      $inv_description=$_POST['rsargs'][7];
    }
    
    if ($masterclass->checkid('`invoices_documents`',$inv_id)!='' && 
      ($inv_qty!='' ||
      $inv_item!='' ||
      $inv_description!='' ||
      $inv_cost!='')
    ) {
      $dbinfo = array(
        'table'=>'invoices_documentitems',
        'static'=>array(
          'qty'=>$inv_qty,
          'name'=>$inv_item,
          'description'=>$inv_description,
          'cost'=>$inv_cost,
          'taxed'=>$inv_tax,
          'taxpercent'=>$inv_taxpercent,
          'invoices'=>$inv_id,
          'personnel'=>$masterclass->site['personnelid'],
          'customer'=>$masterclass->sitevars['customerid']
        )
      );
      $masterclass->db_insert($dbinfo);
    }
    /*
        if (isset($masterclass->sitevars['invoices_document_new_static']) && $masterclass->sitevars['invoices_document_new_static']=='1') {
          $dbinfo = array(
            'table'=>'invoices_documentitems_static',
            'fields'=>array(
              'qty'=>'invoices_documentitem_new_qty',
              'name'=>'invoices_documentitem_new_name',
              'description'=>'invoices_documentitem_new_description',
              'cost'=>'invoices_documentitem_new_cost',
              'taxed'=>'invoices_documentitem_new_taxed',
              'taxpercent'=>'invoices_documentitem_new_taxpercent'
            ),
            'static'=>array(
              'staticlist'=>'default'
            )
          );
        }
        */
  }
  function save_invoiceitem_static() {
    global $masterclass;

    $staticid=$_POST['rsargs'][1];
    $invoiceid=$_POST['rsargs'][2];

    $staticitem=$masterclass->allclasses['invoices']->fetch_invoices_staticitem($staticid);
    if (!empty($staticitem)) {
    $dbinfo = array(
      'table'=>'invoices_documentitems',
      'static'=>array(
        'qty'=>$staticitem['qty'],
        'name'=>$staticitem['name'],
        'description'=>$staticitem['description'],
        'cost'=>$staticitem['cost'],
        'taxed'=>$staticitem['taxed'],
        'taxpercent'=>$staticitem['taxpercent'],
        'personnel'=>$masterclass->site['personnelid'],
        'invoices'=>$invoiceid
      )
    );
    $masterclass->uploaddata($dbinfo);
    }
    
  return;
  }
  function invoices_removeitem() {
    global $masterclass;

    $itemid=$_POST['rsargs'][1];
    $strwhere=sprintf(" `id`=%d ",$masterclass->format_dbl($itemid));

    $dbinfo = array(
      'table'=>'invoices_documentitems',
      'strwhere'=>$strwhere
    );
    $masterclass->db_delete($dbinfo);
  return;
  }
  function generate_invoices_itementry($listarray) {
    global $masterclass;
    
        $pname='';
        $profile='';
        if ($listedids!='') { $listedids.=','; } $listedids.=$listarray->fields['id'];

        if (date("Y-m-d")==date("Y-m-d",strtotime($listarray->fields['updated']))) {
          $pstamp = date("H:m:s",strtotime($listarray->fields['updated']));
        } else {
          $pstamp = date("Y-m-d",strtotime($listarray->fields['updated']));
        }

        if (preg_match('/^[0-9.-]+$/iD', $listarray->fields['qty'])) { $qty=$listarray->fields['qty']; } else { $qty=1; }
        if (preg_match('/^[0-9.-]+$/iD', $listarray->fields['cost'])) { $cost=$listarray->fields['cost']; } else { $cost=0; }
        if (preg_match('/^[0-9.-]+$/iD', $listarray->fields['taxpercent'])) { $taxpercent=$listarray->fields['taxpercent']; } else { $taxpercent=0; }

        if ($cost<0) { $polarity='-'; } else { $polarity='+'; }

        $subtotal=$qty*abs($cost);
        $taxcost=$subtotal*($taxpercent/100);
	    $total=$taxcost+$subtotal;

        $profile_mouseover='OnMouseOver="document.getElementById(\'invoices_itemsbrief'.$listarray->fields['id'].'\').className=\'sitebox_content_mouseover\';document.getElementById(\'invoices_itemsdesc'.$listarray->fields['id'].'\').className=\'sitebox_content_mouseover\';" OnMouseOut="document.getElementById(\'invoices_itemsbrief'.$listarray->fields['id'].'\').className=\'sitebox_content\';document.getElementById(\'invoices_itemsdesc'.$listarray->fields['id'].'\').className=\'sitebox_content\';"';
        //$profile_onclick_open='OnClick="openwindow(\''.$masterclass->mainconfig['dir'].$masterclass->mainconfig['htmldirectory'].'invoices/edititem_default.php?invoicesitemid='.$listarray->fields['id'].'\',\'invoices_item'.$listarray->fields['id'].'\',\'480\',\'360\');"';
        $removeitemclick='OnClick="if (confirm(\'Remove Item\\n'.$listarray->fields['name'].'?\')) { invoices_remove_item(\'invoices_removeitem\',\''.$listarray->fields['id'].'\'); }" ';

        $profile.='<tr id="invoices_itemsbrief'.$listarray->fields['id'].'" valign="top" class="sitebox_content" '.$profile_mouseover.'>';

        $profile.='<td '.$removeitemclick.' width="20" valign="middle" style="padding-left:3px;text-align:left;cursor:pointer;"><img src="'.$masterclass->mainconfig['dir'].'art/cross.png" border="0" title=" Delete "></td>';
        $profile.='<td style="text-align:left;cursor:pointer;padding-top:1px;padding-left:2px;padding-bottom:1px;cursor:pointer;border-right:1px solid;" '.$profile_onclick_open.'>'.$listarray->fields['name'].'</td>';

        //$qtyonclick='OnClick="hide(document.getElementById(\'invoices_edititem_qtytext_'.$listarray->fields['id'].'\'));show(document.getElementById(\'invoices_edititem_qtyinput_'.$listarray->fields['id'].'\'));document.getElementById(\'invoices_edititem_qty\').focus();"';
        $profile.='<td '.$qtyonclick.' width="10" style="text-align:left;padding-top:1px;padding-bottom:1px;cursor:pointer;border-right:1px solid;border-bottom:1px solid;" ><span id="invoices_edititem_qtytext_'.$listarray->fields['id'].'"><nobr>&nbsp;'.$listarray->fields['qty'].'</nobr></span><span id="invoices_edititem_qtyinput_'.$listarray->fields['id'].'" style="display:none;"><input OnFocus="this.select()" style="text-align:right;" type="text" size="3" name="invoices_edititem_qty" id="invoices_edititem_qty" value="'.$listarray->fields['qty'].'" class="sitebox_content"></span></td>';

        $profile.='<td width="20" style="text-align:right;padding-top:1px;padding-bottom:1px;cursor:pointer;border-right:1px solid;border-bottom:1px solid;" align="right" '.$profile_onclick_open.'><nobr>&nbsp;';
        if (preg_match('/^[0-9.-]+$/iD', $cost)) { $profile.='$'.number_format($cost,2); } else { $profile.=$cost; }
        $profile.='</nobr></td>';
        $profile.='<td width="30" style="text-align:right;padding-top:1px;padding-bottom:1px;cursor:pointer;border-right:1px solid;border-bottom:1px solid;" align="right" '.$profile_onclick_open.'><nobr>&nbsp;';
        if ($listarray->fields['taxed']=='1') {
	      if (preg_match('/^[0-9.-]+$/iD', $taxcost)) { $profile.='$'.number_format($taxcost,2).' '; } else { $profile.=$taxcost.' '; }
        } else {
	      $profile.='$0.00 ';
	    }
        $profile.='&nbsp;';
	    if ($listarray->fields['taxed']=='1') { $profile.=$taxpercent; } else { $profile.='0'; }
	    $profile.='%</nobr></td>';
        $profile.='<td width="30" style="text-align:right;padding-top:1px;padding-bottom:1px;cursor:pointer;border-bottom:1px solid;" align="right" '.$profile_onclick_open.'><nobr>&nbsp;';
        if (preg_match('/^[0-9.-]+$/iD', $total)) { $profile.='$'.number_format($total,2); } else { $profile.=$total; }
        $profile.='</nobr></td>';

        $profile.='</tr>';

        if ($listarray->fields['description']!='') {
	      $profile.='<tr id="invoices_itemsdesc'.$listarray->fields['id'].'" '.$profile_mouseover.' style="cursor:pointer;" '.$profile_onclick_open.'>';
	      $profile.='<td colspan="8" style="border-bottom:2px solid;">'.nl2br($listarray->fields['description']).'</td>';
	      $profile.='</tr>';
        } else {
	      $profile.='<tr id="invoices_itemsdesc'.$listarray->fields['id'].'" '.$profile_mouseover.' style="cursor:pointer;border-bottom:1px solid;" '.$profile_onclick_open.'>';
	      $profile.='<td colspan="8" style="border-bottom:2px solid;font-size:2px;">&nbsp;&nbsp;</td>';
	      $profile.='</tr>';
        }

        $ret.=$profile;
  return $profile;
  }
  function display_invoices_itemslist($containername='',$invoicesid='') {
    global $masterclass;

    if (isset($_POST['rsargs']) && !empty($_POST['rsargs'])) {
      $containername=$_POST['rsargs'][1];
      $invoicesid=$_POST['rsargs'][2];
    }

    $listarray=$masterclass->allclasses['invoices']->fetch_invoices_items($invoicesid);
    //$listdisplay=$masterclass->allclasses['invoices']->display_invoices_items($listarray);

    $ret='';
    if (empty($listarray->fields)) {
      $ret.='<table width="100%" align="center" cellpadding="0" cellspacing="0" class="sitebox_content">';
      $ret.='<tr valign="top">';
      $ret.='<td width="100%" valign="middle" style="padding-left:2px;text-align:center;">No Items</td>';
      $ret.='</tr>';
      $ret.='</table>';
    } else {
      $arrinvoicetotal=$masterclass->allclasses['invoices']->calc_invoicetotal($listarray);

      $ret.='<table width="100%" align="center" cellpadding="1" cellspacing="0" class="sitebox_content">';
      $ret.='<tr valign="top" class="sitebox_navigation">';
      $ret.='<td style="text-align:right;" ><nobr>&nbsp;<b>Item Count</b></nobr></td>';
      $ret.='<td style="text-align:right;" ><nobr>&nbsp;<b>SubTotal</b></nobr></td>';
      $ret.='<td style="text-align:right;" ><nobr>&nbsp;<b>Tax</b></nobr></td>';
      $ret.='<td style="text-align:right;" ><nobr>&nbsp;<b>Total</b></nobr></td>';
      $ret.='</tr>';
      $ret.='<tr valign="top" class="sitebox_navigation">';
      $ret.='<td style="text-align:right;border-bottom:1px solid;" ><nobr>&nbsp;<b>'.number_format($arrinvoicetotal['qty'],0).'</b></nobr></td>';
      $ret.='<td style="text-align:right;border-bottom:1px solid;" ><nobr>&nbsp;<b>$'.number_format($arrinvoicetotal['subtotal'],2).'</b></nobr></td>';
      $ret.='<td style="text-align:right;border-bottom:1px solid;" ><nobr>&nbsp;<b>$'.number_format($arrinvoicetotal['tax'],2).'</b></nobr></td>';
      $ret.='<td style="text-align:right;border-bottom:1px solid;" ><nobr>&nbsp;<b>$'.number_format($arrinvoicetotal['total'],2).'</b></nobr></td>';
      $ret.='</tr>';
      $ret.='</table>';


      $ret.='<table width="100%" align="center" cellpadding="1" cellspacing="0" class="sitebox_content">';
      $ret.='<tr valign="top" class="sitebox_navigation">';
      $ret.='<td style="border-right:1px solid;border-bottom:2px solid;" colspan="2"><nobr>&nbsp;<b>Name</b></nobr></td>';
      $ret.='<td width="15" style="text-align:center;border-right:1px solid;border-bottom:2px solid;"><nobr>&nbsp;<b>Qty</b></nobr></td>';
      $ret.='<td width="20" style="text-align:center;border-right:1px solid;border-bottom:2px solid;" ><nobr>&nbsp;<b>Price&nbsp;Per</b></nobr></td>';
      $ret.='<td width="30" style="text-align:center;border-right:1px solid;border-bottom:2px solid;"><nobr>&nbsp;<b>Tax&nbsp;</b></nobr></td>';
      $ret.='<td width="30" style="padding-right:1px;text-align:right;border-bottom:2px solid;"><nobr>&nbsp;<b>Total</b></nobr></td>';
      $ret.='</tr>';
      
      while (!$listarray->EOF) {
        $ret.=$this->generate_invoices_itementry($listarray);      
      $listarray->MoveNext();
      }
      
      $ret.='</table>';
      
      $ret.='<table width="100%" align="center" cellpadding="1" cellspacing="0" class="sitebox_content">';
      $ret.='<tr valign="top" class="sitebox_navigation">';
      $ret.='<td style="text-align:right;" ><nobr>&nbsp;<b>Item Count</b></nobr></td>';
      $ret.='<td style="text-align:right;" ><nobr>&nbsp;<b>SubTotal</b></nobr></td>';
      $ret.='<td style="text-align:right;" ><nobr>&nbsp;<b>Tax</b></nobr></td>';
      $ret.='<td style="text-align:right;" ><nobr>&nbsp;<b>Total</b></nobr></td>';
      $ret.='</tr>';
      $ret.='<tr valign="top" class="sitebox_navigation">';
      $ret.='<td style="text-align:right;border-bottom:1px solid;" ><nobr>&nbsp;<b>'.number_format($arrinvoicetotal['qty'],0).'</b></nobr></td>';
      $ret.='<td style="text-align:right;border-bottom:1px solid;" ><nobr>&nbsp;<b>$'.number_format($arrinvoicetotal['subtotal'],2).'</b></nobr></td>';
      $ret.='<td style="text-align:right;border-bottom:1px solid;" ><nobr>&nbsp;<b>$'.number_format($arrinvoicetotal['tax'],2).'</b></nobr></td>';
      $ret.='<td style="text-align:right;border-bottom:1px solid;" ><nobr>&nbsp;<b>$'.number_format($arrinvoicetotal['total'],2).'</b></nobr></td>';
      $ret.='</tr>';
      $ret.='</table>';
    }

    $arrret['load_list']=$ret;
    $arrret['load_containername']=$containername;
    
    $masterclass->site['grabdata_vars']['load_list']=$ret;
    $masterclass->site['grabdata_vars']['load_containername']=$containername;

  return $arrret;
  }

  function display() {
    global $masterclass;

    $arrdocumentdates=$masterclass->allclasses['invoices']->fetch_invoicesdocumentdates($masterclass->sitevars['invoicesid']);
    $masterclass->sitevars['invoices_documentdate']=$arrdocumentdates[0];
    $masterclass->sitevars['invoices_duedate']=$arrdocumentdates[1];
    $masterclass->sitevars['invoices_paid']=$arrdocumentdates[2];
    
    if ($masterclass->sitevars['invoices_documentdate']=='0000-00-00' || $masterclass->sitevars['invoices_documentdate']=='') { $masterclass->sitevars['invoices_documentdate']=date("Y/m/d"); } else { $masterclass->sitevars['invoices_documentdate']=date("Y/m/d",strtotime($masterclass->sitevars['invoices_documentdate'])); }
    if ($masterclass->sitevars['invoices_duedate']=='0000-00-00' || $masterclass->sitevars['invoices_duedate']=='') { $masterclass->sitevars['invoices_duedate']=''; } else { $masterclass->sitevars['invoices_duedate']=date("Y/m/d",strtotime($masterclass->sitevars['invoices_duedate'])); }
    if ($masterclass->sitevars['invoices_paid']=='0000-00-00' || $masterclass->sitevars['invoices_paid']=='') { $masterclass->sitevars['invoices_paid']=''; } else { $masterclass->sitevars['invoices_paid']=date("Y/m/d",strtotime($masterclass->sitevars['invoices_paid'])); }

  $pageheader='<table width="100%" cellpadding="0" cellspacing="0" border="0" class="sitebox_pageheader"><tr>';
  $pageheader.='<td style="text-align:left;">'.$masterclass->sitevars['invoices_recipientlineone'].'</td>';
  $pageheader.='<td style="text-align:right;" width="25"><nobr>#'.$masterclass->sitevars['invoices_documentid'].'</nobr></td>';
  $pageheader.='</tr></table>';
  echo $masterclass->allclasses['websites']->sitebox_pageheader($pageheader,'');


  $nav='';
  $nav.='<div class="sitebox_navigation" style="width:100%;">';
  $nav.='<ul id="sitebox_navmenu">';
  $nav.='<li OnClick="jsshowloadingscreen();add_post_action(\'invoices_save\');document.getElementById(\'frm\').submit();"><a>Save</a></li>';
  $nav.='<li OnClick="show_overlay(document.getElementById(\'invoices_additem_overlay\'))"><a>Add Item</a></li>';
  $nav.='<li OnClick="jsshowloadingscreen();add_post_action(\'invoices_create\');document.getElementById(\'frm\').submit();"><a>Create Invoice</a></li>';
  /*
  if (isset($masterclass->sitevars['customerid']) && $masterclass->sitevars['customerid']>0) {
    $nav.='<li OnClick="OnClick="openwindow(\''.$masterclass->mainconfig['dir'].$masterclass->mainconfig['htmldirectory'].'invoices/customers_default.php?'.$masterclass->trackingvar_query().'\',\'_blank\',\'760\',\'480\');"><a>Show Customer</a></li>';
  }
  */
  if (isset($masterclass->sitevars['invoicesid']) && $masterclass->sitevars['invoicesid']>0) {
    $nav.='<li OnClick="if (confirm(\'DELETE\n\nAre You Sure?\')) { jsshowloadingscreen();add_post_action(\'invoices_deleteinvoice\');document.getElementById(\'frm\').submit(); }"><a>Delete</a></li>';
  }
  $nav.='<li OnClick="window.close();"><a>Close</a></li>';
  $nav.='</ul>';
  $nav.='<br style="clear: left;" />';
  $nav.='</div>';
  echo $nav;
?>
<table width="100%" cellpadding="2" cellspacing="5" border="0">
  <tr valign="top">
  <td width="40%">
<?php
/* Start Window */
  echo $masterclass->allclasses['websites']->sitebox_start('100%');
  echo $masterclass->allclasses['websites']->sitebox_headline('Invoice Information','');
  
  if ($masterclass->sitevars['invoices_duedate']=='' || $masterclass->sitevars['invoices_duedate']=='0000-00-00') {
    $masterclass->sitevars['invoices_duedate']=$masterclass->sitevars['invoices_documentdate'];
  }

  $calcstamp='calcduedate(document.getElementById(\'invoices_datehidden\'),document.getElementById(\'invoices_net\'),document.getElementById(\'invoices_duehidden\'),\'up\');document.getElementById(\'invoices_duedateframe\').innerHTML=document.getElementById(\'invoices_duehidden\').value,document.getElementById(\'invoices_docdateframe\').innerHTML=document.getElementById(\'invoices_datehidden\').value;';
?>
<script language="javascript">
function calcstamp_wrapper() {
  <?php echo $calcstamp; ?>
}
</script>
<table width="100%" align="center" cellpadding="2" cellspacing="0" class="sitebox_content">
  <tr valign="top">
    <td width="50" valign="middle" style="padding-left:2px;"><nobr>Doc #:&nbsp;</nobr></td>
    <td width="95%"><?php echo $masterclass->allclasses['websites']->disp_inputtext('invoices_documentid','65%','11','10');?>&nbsp;Last: <?php echo $masterclass->allclasses['invoices']->fetch_invoices_lastdocid(); ?></td>
  </tr>
   <tr valign="top">
     <td width="50" valign="middle" style="padding-left:2px;"><nobr>Date:&nbsp;</nobr></td>
     <td width="95%" valign="middle"><span id="invoices_docdateframe"><?php echo $masterclass->allclasses['websites']->disp_text('invoices_documentdate');?></span>&nbsp;<img OnClick="show_calendar('frm.invoices_datehidden',document.getElementById('invoices_datehidden').value,'self.opener.calcstamp_wrapper();');" style="cursor:pointer;" align="absmiddle" src="<?php echo $masterclass->mainconfig['dir'];?>art/calendar.png" border="0"></td>
   </tr>
   <tr valign="absmiddle">
    <td width="50" valign="middle" style="padding-left:2px;"><nobr>Terms:&nbsp;</nobr></td>
    <td width="95%" colspan="3"><?php
    $options=array();
    $options[]=array('0','Upon Receipt');
    $options[]=array('5','Net 5');
    $options[]=array('10','Net 10');
    $options[]=array('15','Net 15');
    $options[]=array('30','Net 30');
    $options[]=array('45','Net 45');
    $options[]=array('90','Net 90');
    echo $masterclass->allclasses['websites']->disp_select($options,'invoices_net',$masterclass->sitevars['invoices_net'],'65%',$calcstamp);
    ?>&nbsp;</td>
   </tr>
   <tr valign="top">
     <td width="50" valign="middle" style="padding-left:2px;"><nobr>Due&nbsp;Date:&nbsp;</nobr></td>
     <td width="95%" valign="middle" id="invoices_duedateframe"><?php echo $masterclass->allclasses['websites']->disp_text('invoices_duedate');?>&nbsp;</td>
    </tr>
   <tr valign="top">
     <td width="50" valign="middle" style="padding-left:2px;"><nobr>Paid:&nbsp;</nobr></td>
     <td width="95%" valign="middle"><?php echo $masterclass->allclasses['websites']->disp_text('invoices_paid');?>&nbsp;</td>
    </tr>
  </table>
  <input type="hidden" name="invoices_duehidden" id="invoices_duehidden" value="<?php echo $masterclass->allclasses['websites']->disp_text('invoices_duedate');?>">
  <input type="hidden" OnChange="<?php echo $calcstamp;?>" name="invoices_datehidden" id="invoices_datehidden" value="<?php echo $masterclass->allclasses['websites']->disp_text('invoices_documentdate');?>">
<?php
  echo $masterclass->allclasses['websites']->sitebox_end();
/* End Window */
  echo $masterclass->allclasses['websites']->sitebox_vspacer();
/* Start Window */
  echo $masterclass->allclasses['websites']->sitebox_start('100%');
  echo $masterclass->allclasses['websites']->sitebox_headline('Billing Information','');
?>
<table width="100%" align="center" cellpadding="0" cellspacing="1" class="sitebox_content">
<!--
  <tr valign="top">
    <td width="95%"><b>Company:</b></td>
  </tr>
  <tr valign="top">
    <td width="95%">&nbsp;&nbsp;<?php echo $masterclass->allclasses['websites']->disp_text('invoices_companylineone');?></td>
  </tr>
  <tr valign="top">
    <td width="95%">&nbsp;&nbsp;<?php echo $masterclass->allclasses['websites']->disp_text('invoices_companylinetwo');?></td>
  </tr>
  <tr valign="top">
    <td width="95%">&nbsp;&nbsp;<?php echo $masterclass->allclasses['websites']->disp_text('invoices_companylinethree');?></td>
  </tr>
  <tr valign="top">
    <td width="95%">&nbsp;&nbsp;<?php echo $masterclass->allclasses['websites']->disp_text('invoices_companylinefour');?></td>
  </tr>
  <tr valign="top">
    <td width="95%">&nbsp;&nbsp;<?php echo $masterclass->allclasses['websites']->disp_text('invoices_companylinefive');?></td>
  </tr>
-->
  <tr valign="top">
    <td width="95%"><b>Recipient Information:</b></td>
  </tr>
  <tr valign="top">
     <td width="95%"><?php echo $masterclass->allclasses['websites']->disp_inputtext('invoices_recipientlineone');?></td>
  </tr>
  <tr valign="top">
    <td width="95%"><?php echo $masterclass->allclasses['websites']->disp_inputtext('invoices_recipientlinetwo');?></td>
  </tr>
  <tr valign="top">
    <td width="95%"><?php echo $masterclass->allclasses['websites']->disp_inputtext('invoices_recipientlinethree');?></td>
  </tr>
  <tr valign="top">
    <td width="95%"><?php echo $masterclass->allclasses['websites']->disp_inputtext('invoices_recipientlinefour');?></td>
  </tr>
  <tr valign="top">
    <td width="95%"><?php echo $masterclass->allclasses['websites']->disp_inputtext('invoices_recipientlinefive');?></td>
  </tr>
</table>
<?php
  echo $masterclass->allclasses['websites']->sitebox_end();
/* End Window */
echo $masterclass->allclasses['websites']->sitebox_vspacer();
/* Start Window */
  echo $masterclass->allclasses['websites']->sitebox_start('100%');
  echo $masterclass->allclasses['websites']->sitebox_headline('Reusable Items','');

$staticitems=$masterclass->allclasses['invoices']->fetch_invoices_staticitems('default');

if (!empty($staticitems->fields)) {
?>
<table width="100%" align="center" cellpadding="1" cellspacing="0" class="sitebox_content">
<?php
  while (!$staticitems->EOF) {
    if ($staticitems->fields['qty']<1) { $itemqty=1; } else { $itemqty=$staticitems->fields['qty']; }
    $itemcost=$staticitems->fields['cost']*$itemqty;
?>
  <tr valign="top" style="cursor:pointer;" OnClick="invoices_additem_static('save_invoiceitem_static','<?php echo $staticitems->fields['id'];?>','<?php echo $masterclass->sitevars['invoicesid'];?>');" OnMouseOver="this.className='sitebox_content_mouseover';" OnMouseOut="this.className='sitebox_content';" Class="sitebox_content">
    <td width="10" style="border-bottom:1px solid;">&nbsp;&nbsp;<?php echo $itemqty;?></td>
    <td style="border-bottom:1px solid;">&nbsp;&nbsp;<?php echo $staticitems->fields['name']; ?></td>
    <td width="50" style="text-align:right;border-bottom:1px solid;">&nbsp;&nbsp;$<?php echo number_format($itemcost,2); ?></td>
  </tr>
<?php
    $staticitems->MoveNext();
  }
?>
</table>
<?php
}
  echo $masterclass->allclasses['websites']->sitebox_end();
/* End Window */
?>
</td><td width="60%">
<?php
/* Start Window */
  echo $masterclass->allclasses['websites']->sitebox_start('100%');
  echo $masterclass->allclasses['websites']->sitebox_headline('Invoice Items','');
  
  $unquieid='invoice_items_container';
  $invoices_itemsclick='update_'.$unquieid.'();';

  $runcmdpre='';
  $cngpg_cmd="invoices_itemslist('display_invoices_itemslist','invoice_items_container','".$masterclass->sitevars['invoicesid']."');";
  $runcmdpost=$cngpg_cmd;
  
  $listdata=$this->display_invoices_itemslist($unquieid,$masterclass->sitevars['invoicesid']);
?>
<div id="<?php echo $unquieid; ?>"><?php echo $listdata['load_list']; ?></div>
<script language="javascript">
  var <?php echo $unquieid; ?>_listview='';
  function update_<?php echo $unquieid;?>() {
    <?php echo $cngpg_cmd; ?>
  }
</script>
<?php
  echo $masterclass->allclasses['websites']->sitebox_end();
/* End Window */
?>
</td></tr></table>
<?php


/*********************/
/* OVERLAY WINDOWS */
/*********************/

/* Start Window */
  $saveonclick="invoices_additem('save_invoiceitem','".$masterclass->sitevars['invoicesid']."',document.getElementById('invoices_documentitem_new_qty'),document.getElementById('invoices_documentitem_new_cost'),document.getElementById('invoices_documentitem_new_taxed'),document.getElementById('invoices_documentitem_new_taxpercent'),document.getElementById('invoices_documentitem_new_name'),document.getElementById('invoices_documentitem_new_description'));hide_overlay(document.getElementById('invoices_additem_overlay'));";
  echo $masterclass->allclasses['websites']->liftbox_start('invoices_additem_overlay','300');
  echo $masterclass->allclasses['websites']->liftbox_headline('invoices_additem_overlay','&nbsp;New Item');
?>
    <table width="100%" align="center" cellpadding="5" cellspacing="0">
      <tr valign="top"><td valign="top">
        <table width="100%" cellpadding="0" cellspacing="0" border="0" class="sitebox_content">
          <tr>
            <td>Qty</td>
            <td>Cost Each</td>
            <td>Tax</td>
          </tr>
          <tr>
            <td><?php echo $masterclass->allclasses['websites']->disp_inputtext('invoices_documentitem_new_qty','','8','6');?></td>
            <td><?php echo $masterclass->allclasses['websites']->disp_inputtext('invoices_documentitem_new_cost','','15','8');?></td>
            <td><?php echo $masterclass->allclasses['websites']->disp_inputcheckbox('invoices_documentitem_new_taxed','1');?>&nbsp;@&nbsp;<?php echo $masterclass->allclasses['websites']->disp_inputtext('invoices_documentitem_new_taxpercent','','6','5');?>%</td>
          </tr>
        </table>
      <tr valign="top"><td valign="top">
        Item:&nbsp;<br><?php echo $masterclass->allclasses['websites']->disp_inputtext('invoices_documentitem_new_name','95%');?><br>
      </td></tr>
      <tr valign="top"><td valign="top">
        Description:&nbsp;<br><?php echo $masterclass->allclasses['websites']->disp_textarea('invoices_documentitem_new_description','95%','3','40');?><br>
      </td></tr>
      <tr valign="top"><td valign="top">        
        <div style="text-align:center;"><input type="button" class="button" style="text-align:center;width:80%;" align="center" value="Save" OnClick="<?php echo $saveonclick; ?>"></div>
      </td></tr>
    </table>
<?php

  echo $masterclass->allclasses['websites']->liftbox_end();
/* End Window */




  return;
  }
}
?>